package com.yjz.study.ui.biz

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.yjz.common.base.BaseActivity
import com.yjz.common.callback.ToastCallBack
import com.yjz.common.extend.back
import com.yjz.common.extend.getResStr
import com.yjz.common.extend.showToast
import com.yjz.log.JLog
import com.yjz.study.R
import com.yjz.study.account.AccountManager
import com.yjz.study.arouter.ARouterLogic
import com.yjz.study.arouter.navigation
import com.yjz.study.http.entity.Resource
import com.yjz.study.viewmodel.BizViewModel
import com.yjz.ui.view.InputItemLayout

/**
 * 作用 :
 * 作者 : yjz
 * 时间 : 2024/10/24 14:59
 */
@Route(path = ARouterLogic.Path.LOGIN_PATH)
class LoginActivity : BaseActivity(), View.OnClickListener {

    companion object {
        const val REQUEST_CODE_REGISTRATION = 1000
    }

    private lateinit var userNameEdt: InputItemLayout
    private lateinit var pwdEdt: InputItemLayout
    private lateinit var bizViewModel: BizViewModel

    override fun getLayoutId() = R.layout.activity_login

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        findViewById<View>(R.id.action_login).setOnClickListener(this)
        findViewById<View>(R.id.action_back).setOnClickListener(this)
        findViewById<View>(R.id.action_register).setOnClickListener(this)

        bizViewModel = ViewModelProvider.NewInstanceFactory().create(BizViewModel::class.java)

        userNameEdt = findViewById(R.id.input_item_username)
        pwdEdt = findViewById(R.id.input_item_password)
        userNameEdt.setText("imooc")
        pwdEdt.setText("123456")

    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.action_login -> {

                val username = userNameEdt.getTextContent()
                val pwd = pwdEdt.getTextContent()
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(pwd)) {
                    showToast(getResStr(R.string.input_empty_hint))
                    return
                }

                bizViewModel.login(username, pwd).observe(this, Observer { source ->
                    when (source.status) {
                        Resource.Status.LOADING -> {
                            // TODO showLoadingDialog
                            JLog.eTag("loading", "showLoading")
                        }

                        Resource.Status.SUCCESS -> {
                            // TODO closeLoadingDialog
                            JLog.eTag("loading", "success closeLoadingDialog")
                            ToastCallBack.toast(source.msg)
                            AccountManager.loginSuccess(source.data!!)
                            finish()
                        }

                        Resource.Status.ERROR -> {
                            // TODO closeLoadingDialog
                            ToastCallBack.toast(source.msg)
                            JLog.eTag("loading", "error closeLoadingDialog")
                        }
                    }
                })
            }

            R.id.action_register -> navigation(ARouterLogic.Path.REGISTER_PATH, REQUEST_CODE_REGISTRATION)

            R.id.action_back -> back()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data != null && requestCode == REQUEST_CODE_REGISTRATION) {
            val username = data.getStringExtra("username")
            userNameEdt.setText(username)
        }
    }
}
